package com.example.networklab.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.example.networklab.common.ResultResponse;
import com.example.networklab.domain.req.AddUserReq;
import com.example.networklab.domain.req.EditSelfReq;
import com.example.networklab.domain.req.LoginReq;
import com.example.networklab.domain.req.RegisterBatchReq;
import com.example.networklab.service.IUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author dzw
 * @since 2025-02-26
 */
@CrossOrigin(origins = "*")
@RestController
@RequiredArgsConstructor
public class UserController {
    private final IUserService userService;

    //登录
    @PostMapping("/login")
    public ResultResponse login(@RequestBody @Validated LoginReq loginReq) {
        return ResultResponse.success(userService.login(loginReq));
    }

    //用户自己编辑自己的信息
    @PutMapping("/user/self/edit")
    @SaCheckLogin
    public ResultResponse editSelf(@RequestBody EditSelfReq editSelfReq) {
        userService.editSelf(editSelfReq);
        return ResultResponse.success();
    }

    //新增用户
    @PostMapping("/register")
    @SaCheckLogin
    @SaCheckRole("teacher")
    public ResultResponse register(@RequestBody @Validated AddUserReq addUserReq) {
        userService.register(addUserReq);
        return ResultResponse.success();
    }

    //批量注册新用户
    @PostMapping("/register/batch")
    @SaCheckLogin
    @SaCheckRole("teacher")
    public ResultResponse registerBatch(@RequestBody @Validated RegisterBatchReq registerBatchReq) {
        userService.registerBatch(registerBatchReq);
        return ResultResponse.success();
    }
}
